package com.turing.post.account.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.account.util.OnlineLoadDetail;
import com.turing.post.account.util.OnlineLoadDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "account.impl.OnlineLoadDetailDaoImpl")
@Remote
@Local
public class OnlineLoadDetailDaoImpl implements OnlineLoadDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public OnlineLoadDetail getOnlineLoadDetailByLocalSeqNo(String loadSeqNo) {
		return _em.find(OnlineLoadDetail.class, loadSeqNo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OnlineLoadDetail> getOnlineLoadDetailList(
			PCPSWebInput webInput, String loadSeqNo, String cardNo,
			String saleBranchNo, String startTime, String endTime,
			String tranType, String status, Integer page) {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select t.* from OnlineLoadDetail t"
				+ " where t.issuecompanyno='" + issueCompanyNo + "'"
				+ " and (t.loadSeqNo = ?1 or ?1 is null)"
				+ " and t.trantime >= to_date(?2,'yyyy-mm-dd hh24:mi:ss') "
				+ " and t.trantime <= to_date(?3,'yyyy-mm-dd hh24:mi:ss') "
				+ " and (t.trantype = ?4 or ?4 is null)"
				+ " and (t.status = ?5 or ?5 is null)"
				+ " and (t.cardno = ?6 or ?6 is null)"
				+ " order by t.trantime desc";
		Query query = _em.createNativeQuery(sql, OnlineLoadDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		query.setParameter(1, loadSeqNo);
		query.setParameter(2, startTime + " 00:00:00");
		query.setParameter(3, endTime + " 23:59:59");
		query.setParameter(4, tranType);
		query.setParameter(5, status);
		query.setParameter(6, cardNo);
		// 判断是查询全部还是选定机构
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "SaleBranchNo");

		return query.getResultList();
	}

	@Override
	public BigInteger getOnlineLoadDetailTotal(PCPSWebInput webInput,
			String loadSeqNo, String cardNo, String saleBranchNo,
			String startTime, String endTime, String tranType, String status) {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0) from OnlineLoadDetail t"
				+ " where t.issuecompanyno='" + issueCompanyNo + "'"
				+ " and (t.loadSeqNo = ?1 or ?1 is null)"
				+ " and t.trantime >= to_date(?2,'yyyy-mm-dd hh24:mi:ss') "
				+ " and t.trantime <= to_date(?3,'yyyy-mm-dd hh24:mi:ss') "
				+ " and (t.trantype = ?4 or ?4 is null)"
				+ " and (t.status = ?5 or ?5 is null)"
				+ " and (t.cardno = ?6 or ?6 is null)"
				+ " order by t.trantime desc";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, loadSeqNo);
		query.setParameter(2, startTime + " 00:00:00");
		query.setParameter(3, endTime + " 23:59:59");
		query.setParameter(4, tranType);
		query.setParameter(5, status);
		query.setParameter(6, cardNo);
		// 判断是查询全部还是选定机构
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "SaleBranchNo");

		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insert(OnlineLoadDetail oldobj) throws PCPSApplicationException {
		GeneralDao.insert(_em, oldobj);
	}

	@Override
	public void update(OnlineLoadDetail oldobj) throws PCPSApplicationException {
		GeneralDao.update(_em, oldobj);
	}

	/**
	 * 更新(在单独的事务中)
	 * 
	 * @param oldobj
	 * @throws PCPSApplicationException
	 */
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void updateCommit(OnlineLoadDetail oldobj)
			throws PCPSApplicationException {
		GeneralDao.update(_em, oldobj);
	}
}
